/**
 * 
 */
package org.atmaca.ert.service;

import java.io.IOException;
import java.util.concurrent.Callable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.sun.syndication.io.FeedException;

/**
 * @author "huseyin.atmaca"
 * 
 */

@Service
public class BatchOperationServiceImpl {

	@Autowired
	private UpdateRssServiceImpl updateRssService;

	@Autowired
	private ThreadPoolTaskExecutor batchTaskExecutor;

	private final Log logger = LogFactory
			.getLog(BatchOperationServiceImpl.class);

	@Scheduled(cron = "0 * * * * ?")
	public void checkXml() throws IOException, IllegalArgumentException,
			FeedException {
		fireSabah();
	}

	private void fireSabah() {
		batchTaskExecutor.submit(new Callable<Boolean>() {
			@Override
			public Boolean call() {
				try {
					executeSabah();
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					return false;
				}
				return true;
			}

		});
	}

	private void executeSabah() throws IOException, IllegalArgumentException,
			FeedException {
		
		updateRssService.updateLink("http://www.sabah.com.tr/rss/Anasayfa.xml");
	}

}
